package admin

import (
	"bbs-go/internal/models"
	"bbs-go/internal/pkg/errs"
	"bbs-go/internal/services"
	"strconv"

	"github.com/kataras/iris/v12"
	"github.com/mlogclub/simple/web"
	"github.com/mlogclub/simple/web/params"
)

type ApprovalProcessController struct {
	Ctx iris.Context
}

func (c *ApprovalProcessController) PostCreate() *web.JsonResult {
	user := services.UserTokenService.GetCurrent(c.Ctx)
	if user == nil {
		return web.JsonError(errs.NotLogin)
	}
	t := &models.ApprovalProcess{}
	err := params.ReadForm(c.Ctx, t)
	if err != nil {
		return web.JsonError(err)
	}
	err = services.ApprovalProcessService.Create(t)
	if err != nil {
		return web.JsonError(err)
	}
	return web.JsonData(t)
}

func (c *ApprovalProcessController) AnyList() *web.JsonResult {
	list, paging := services.ApprovalProcessService.FindPageByParams(params.NewQueryParams(c.Ctx).EqByReq("index_category_id").LikeByReq("title").LikeByReq("summary").EqByReq("status").PageByReq().Asc("sort"))
	return web.JsonData(&web.PageResult{Results: list, Page: paging})
}

func (c *ApprovalProcessController) PostUpdate() *web.JsonResult {
	id, err := params.FormValueInt64(c.Ctx, "id")
	if err != nil {
		return web.JsonErrorMsg(err.Error())
	}
	t := services.ApprovalProcessService.Get(id)
	if t == nil {
		return web.JsonErrorMsg("entity not found")
	}

	err = params.ReadForm(c.Ctx, t)
	if err != nil {
		return web.JsonErrorMsg(err.Error())
	}

	err = services.ApprovalProcessService.Update(t)
	if err != nil {
		return web.JsonErrorMsg(err.Error())
	}
	return web.JsonData(t)
}

func (c *ApprovalProcessController) GetBy(id int64) *web.JsonResult {
	t := services.ApprovalProcessService.Get(id)
	if t == nil {
		return web.JsonErrorMsg("Not found, id=" + strconv.FormatInt(id, 10))
	}
	return web.JsonData(t)
}

// func (c *ApprovalProcessController) PostDelete() *web.JsonResult {
// 	id, _ := params.FormValueInt64(c.Ctx, "id")
// 	if id <= 0 {
// 		return web.JsonError(fmt.Errorf("请输入id"))
// 	}
// 	resourceTypes := services.ApprovalProcessService.Get(id)
// 	if resourceTypes == nil {
// 		return web.JsonError(fmt.Errorf("资源不存在"))
// 	}

// 	err := services.ApprovalProcessService.Delete(resourceTypes.Id)
// 	if err != nil {
// 		return web.JsonError(err)
// 	}
// 	return web.JsonSuccess()
// }
